# -*- python -*-
# ex: set syntax=python:

# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from common import chromium_utils

from buildbot.scheduler import Nightly
from buildbot.scheduler import Triggerable
from buildbot.schedulers.basic import SingleBranchScheduler

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory
from master.factory import chromium_factory

import config
import master_site_config

ActiveMaster = master_site_config.V8FYI
MAIL_NOTIFIER = ActiveMaster.is_production_host

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []


####### DATABASE

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)


####### CHANGESOURCES

comparator = gitiles_poller.GitilesRevisionComparator()
c['change_source'] = [
  gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/v8/v8',
      branches=['master', 'candidate'],
      pollInterval=20,
      comparator=comparator,
  )
]


####### FACTORIES

F = annotator_factory.AnnotatorFactory().BaseFactory

bleeding_edge_url = 'http://v8.googlecode.com/svn/branches/bleeding_edge'
bleeding_edge_rev_url = '%s@$$V8_REV$$' % bleeding_edge_url

cr_lkcr = 'https://build.chromium.org/p/chromium/lkcr-status/lkgr'

def linux(): return chromium_factory.ChromiumFactory(
    'src/out', 'linux2',
    custom_deps_list=[['src/v8', bleeding_edge_rev_url]],
    got_revision_mapping_overrides={
      'src/v8': 'got_revision',
      'src': 'got_cr_revision',
    })

f_chromium_linux_snapshot_builder = linux().ChromiumFactory(
    slave_type='Builder',
    options=[
      '--build-tool=ninja',
      '--compiler=goma',
    ],
    factory_properties={
      'archive_build': True,
      'build_name': 'Linux_x64',
      'gclient_env': { 'GYP_GENERATORS': 'ninja' },
      'gs_bucket': 'gs://chromium-v8-snapshots',
      'no_gclient_revision': True,
      'primary_repo': 'v8_',
      'revision_dir': 'v8',
      'safesync_url': cr_lkcr,
    })


####### BUILDER SPECS

CATEGORY_RELEASE = '1Release|release'
CATEGORY_LAYOUT = '2Layout|chromium'
CATEGORY_NONLAYOUT = '3Nonlayout|chromium'
CATEGORY_NONLAYOUT_NO_MAIL = '3Nonlayout'
CATEGORY_PERF = '4Performance'
CATEGORY_CLUSTERFUZZ = '5Clusterfuzz|clusterfuzz'

nightly_builders = [
  {
    'name': 'Auto-roll - push',
    'builddir': 'auto-roll-push',
    'factory': F('v8/auto_roll_push'),
    'category': CATEGORY_RELEASE,
    'auto_reboot' : False,
  },
  {
    'name': 'Auto-roll - deps',
    'builddir': 'auto-roll-deps',
    'factory': F('v8/auto_roll_deps'),
    'category': CATEGORY_RELEASE,
    'auto_reboot' : False,
  },
  {
    'name': 'Auto-roll - v8 deps',
    'builddir': 'auto-roll-v8-deps',
    'factory': F('v8/auto_roll_v8_deps'),
    'category': CATEGORY_RELEASE,
    'auto_reboot' : False,
  },
  {
    'name': 'Auto-roll - v8rel',
    'builddir': 'v8rel',
    'factory': F('v8/auto_roll_v8rel'),
    'category': CATEGORY_RELEASE,
    'auto_reboot' : False,
  },
  {
    'name': 'Auto-roll - release process',
    'builddir': 'release',
    'factory': F('v8/auto_roll_release_process'),
    'category': CATEGORY_RELEASE + '|clusterfuzz',
    'auto_reboot' : False,
  }
]


scheduled_builders = [
  {
    'name': 'V8-Blink Win',
    'factory': F('blink_downstream'),
    'category': CATEGORY_LAYOUT,
  },
  {
    'name': 'V8-Blink Mac',
    'factory': F('blink_downstream'),
    'category': CATEGORY_LAYOUT,
  },
  {
    'name': 'V8-Blink Linux 32',
    'factory': F('blink_downstream'),
    'category': CATEGORY_LAYOUT,
  },
  {
    'name': 'V8-Blink Linux 64',
    'factory': F('blink_downstream'),
    'category': CATEGORY_LAYOUT,
  },
  {
    'name': 'V8-Blink Linux 64 (dbg)',
    'factory': F('blink_downstream'),
    'category': CATEGORY_LAYOUT,
  },
  {
    'name': 'Linux Debug Builder',
    'factory': F('chromium', triggers=['cr_linux_dbg']),
    'category': CATEGORY_NONLAYOUT,
  },
  {
    'name': 'V8 Linux GN',
    'factory': F('chromium_gn'),
    'category': CATEGORY_NONLAYOUT,
  },
  {
    'name': 'V8 Android GN (dbg)',
    'factory': F('chromium_gn'),
    'category': CATEGORY_NONLAYOUT,
  },
  {
    'name': 'Linux ASAN Builder',
    'factory': F('chromium', triggers=['cr_linux_asan']),
    'category': CATEGORY_NONLAYOUT,
  },
  {
    'name': 'Android Builder',
    'factory': F('android/builder', triggers=['cr_android_rel']),
    'category': CATEGORY_NONLAYOUT,
  },
  {
    'name': 'Linux Snapshot Builder',
    'factory': f_chromium_linux_snapshot_builder,
    'category': CATEGORY_NONLAYOUT_NO_MAIL,
  },
  {
    'name': 'Chrome Linux Perf',
    'factory': F('chromium'),
    'category': CATEGORY_PERF,
  },
  {
    'name': 'Chrome Mac10.9 Perf',
    'factory': F('chromium'),
    'category': CATEGORY_PERF,
  },
  {
    'name': 'Chrome Win7 Perf',
    'factory': F('chromium', timeout=3600),
    'category': CATEGORY_PERF,
  },
]

clusterfuzz_builders = [
  {
    'name': 'Chromium ASAN (symbolized)',
    'factory': F('chromium'),
    'category': CATEGORY_CLUSTERFUZZ,
  },
  {
    'name': 'Chromium ASAN - debug',
    'factory': F('chromium'),
    'category': CATEGORY_CLUSTERFUZZ,
  },
  {
    'name': 'Chromium ASAN arm64 - debug',
    'factory': F('chromium'),
    'category': CATEGORY_CLUSTERFUZZ,
  },
  {
    'name': 'Chromium ASAN mipsel - debug',
    'factory': F('chromium'),
    'category': CATEGORY_CLUSTERFUZZ,
  },
  {
    'name': 'Chromium Win SyzyASAN',
    'factory': F('chromium', timeout=3600),
    'category': CATEGORY_CLUSTERFUZZ,
  },
]

triggered_builders = [
  {
    'name': 'Linux Tests (dbg)(1)',
    'factory': F('chromium'),
    'category': CATEGORY_NONLAYOUT_NO_MAIL,
  },
  {
    'name': 'Linux ASan LSan Tests (1)',
    'factory': F('chromium'),
    'category': CATEGORY_NONLAYOUT_NO_MAIL,
  },
  {
    'name': 'Android Nexus4 Perf',
    'factory': F('android/perf'),
    'category': CATEGORY_PERF,
  },
]


####### SCHEDULERS

c['schedulers'] = [
  SingleBranchScheduler(
      name='v8_fyi',
      branch='master',
      treeStableTimer=10,
      builderNames=[builder['name'] for builder in scheduled_builders],
  ),
  SingleBranchScheduler(
      name='v8_candidate',
      branch='candidate',
      treeStableTimer=10,
      builderNames=[builder['name'] for builder in clusterfuzz_builders],
  ),
  Triggerable('cr_linux_dbg', ['Linux Tests (dbg)(1)']),
  Triggerable('cr_linux_asan', ['Linux ASan LSan Tests (1)']),
  Triggerable('cr_android_rel', ['Android Nexus4 Perf']),
  # Auto-roll builders.
  Nightly(
      name='v8_auto_push',
      branch=None,
      builderNames=['Auto-roll - push'],
      minute=[10, 25, 40, 55],
  ),
  Nightly(
      name='v8_v8rel',
      branch=None,
      builderNames=[
        'Auto-roll - release process',
        'Auto-roll - v8rel',
        'Auto-roll - deps',
      ],
      minute=[0, 15, 30, 45],
  ),
  # Run v8 deps roller every EMEA morning at 5am.
  Nightly(
      name='v8_v8deps',
      branch=None,
      builderNames=['Auto-roll - v8 deps'],
      hour=[20],
  ),
]

####### BUILDERS

c['builders'] = (
    nightly_builders +
    scheduled_builders +
    triggered_builders +
    clusterfuzz_builders
)

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'V8FYI')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=comparator,
    templates=['../master.client.v8/templates',
               '../master.chromium/templates'])

if MAIL_NOTIFIER:
  import mail_notifier_cfg
  mail_notifier_cfg.Update(config, ActiveMaster, c)

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
